package com.atguigu.gulimall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gulimall.feign.seckill.SeckillFeignClient;
import com.atguigu.gulimall.feign.user.UserFeignClient;
import com.atguigu.gulimall.order.entity.OrderInfo;
import com.atguigu.gulimall.seckill.entity.SeckillGoods;
import com.atguigu.gulimall.user.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author nicc
 * @version 1.0
 * @className SeckillController
 * @date 2022-10-27 16:42
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillFeignClient seckillFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 秒杀商品页
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillPage(Model model){

        //远程调用拿到当天需要参与秒杀的所有商品
        Result<List<SeckillGoods>> seckillGoods = seckillFeignClient.getCurrentDaySeckillGoods();

        model.addAttribute("list",seckillGoods.getData());

        return "seckill/index";
    }

    /**
     * 查询秒杀商品详情
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillGoodsDetail(@PathVariable("skuId") Long skuId,
                                     Model model){

        Result<SeckillGoods> detail = seckillFeignClient.getDetail(skuId);
        model.addAttribute("item", detail.getData());

        return "seckill/item";
    }

    /**
     * 秒杀排队页
     * @param skuId
     * @param skuIdStr
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String queue(@RequestParam("skuId") Long skuId,
                        @RequestParam("skuIdStr") String skuIdStr,
                        Model model){

        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }

    /**
     * 秒杀单确认页
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String trade(@RequestParam("skuId") Long skuId,
                        Model model){
        //1、查询用户收货地址
        Result<List<UserAddress>> userAddress = userFeignClient.getUserAddress();
        model.addAttribute("userAddressList",userAddress.getData());


        //2、查询秒杀订单
        Result<OrderInfo> seckillOrder = seckillFeignClient.getSeckillOrder(skuId);
        OrderInfo orderInfo = seckillOrder.getData();

        model.addAttribute("detailArrayList",orderInfo.getOrderDetailList());
        model.addAttribute("totalNum",1);
        model.addAttribute("totalAmount",orderInfo.getTotalAmount());
        return "seckill/trade";
    }

}
